package com.rfid.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.rfid.common.exception.BusinessException;
import com.rfid.common.utils.DateUtils;
import com.rfid.common.utils.StringUtils;
import com.rfid.system.domain.TCustomerInfo;
import com.rfid.system.domain.TProductInfo;
import com.rfid.system.domain.response.OrderInfoResponseVO;
import com.rfid.system.mapper.TCustomerInfoMapper;
import com.rfid.system.service.common.CodeRfidGenerateService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rfid.system.mapper.TOrderTotalMapper;
import com.rfid.system.domain.TOrderTotal;
import com.rfid.system.service.ITOrderTotalService;
import com.rfid.common.core.text.Convert;

/**
 * 单据总Service业务层处理
 *
 * @author fangchen
 * @date 2020-12-24
 */
@Service
public class TOrderTotalServiceImpl implements ITOrderTotalService {
    @Autowired
    private TOrderTotalMapper tOrderTotalMapper;

    @Autowired
    private CodeRfidGenerateService codeGenerateService;

    @Autowired
    private TCustomerInfoMapper tCustomerInfoMapper;
    /**
     * 查询单据总
     *
     * @param id 单据总ID
     * @return 单据总
     */
    @Override
    public TOrderTotal selectTOrderTotalById(Long id) {
        return tOrderTotalMapper.selectTOrderTotalById(id);
    }

    /**
     * 查询单据总列表
     *
     * @param tOrderTotal 单据总
     * @return 单据总
     */
    @Override
    public List<TOrderTotal> selectTOrderTotalList(TOrderTotal tOrderTotal) {
        return tOrderTotalMapper.selectTOrderTotalList(tOrderTotal);
    }

    /**
     * 新增单据总
     *
     * @param tOrderTotal 单据总
     * @return 结果
     */
    @Override
    public int insertTOrderTotal(TOrderTotal tOrderTotal) {
        tOrderTotal.setCreateTime(DateUtils.getNowDate());
        return tOrderTotalMapper.insertTOrderTotal(tOrderTotal);
    }

    /**
     * 修改单据总
     *
     * @param tOrderTotal 单据总
     * @return 结果
     */
    @Override
    public int updateTOrderTotal(TOrderTotal tOrderTotal) {
        tOrderTotal.setUpdateTime(DateUtils.getNowDate());
        return tOrderTotalMapper.updateTOrderTotal(tOrderTotal);
    }

    /**
     * 删除单据总对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTOrderTotalByIds(String ids) {
        return tOrderTotalMapper.deleteTOrderTotalByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除单据总信息
     *
     * @param id 单据总ID
     * @return 结果
     */
    @Override
    public int deleteTOrderTotalById(Long id) {
        return tOrderTotalMapper.deleteTOrderTotalById(id);
    }

    @Override
    public OrderInfoResponseVO selectOrderInfo(String orderNo) {
        return tOrderTotalMapper.selectOrderInfo( orderNo);
    }

    @Override
    public List<Map<String, Object>> getProductInfo(String orderNo) {
        return tOrderTotalMapper.getProductInfo(orderNo);
    }

    @Override
    public TOrderTotal getLastData() {
        return tOrderTotalMapper.getLastData();
    }

    @Override
    public String importUser(List<TOrderTotal> orderTotals, boolean updateSupport, String operName) {
        if (StringUtils.isNull(orderTotals) || orderTotals.size() == 0) {
            throw new BusinessException("导入单据数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int countFail=0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TOrderTotal orderTotal : orderTotals){
            String inertFlag="0";
            int count=0;
            if(StringUtils.isEmpty(orderTotal.getCustomerNo())){
                count++;
                inertFlag="1";
                failureNum++;
                countFail++;
                failureMsg.append("<br/>" + failureNum + "、外部客户编号不能为空");
            }
            if(orderTotal.getOrderType()==null){
                if(count==0){
                    count++;
                    countFail++;
                }
                inertFlag="1";
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、单据类型编号不能为空");
            }
            TCustomerInfo tCustomerInfo=new TCustomerInfo();
            tCustomerInfo.setOutCstno(orderTotal.getCustomerNo());
            List<TCustomerInfo> list=tCustomerInfoMapper.selectTCustomerInfoList(tCustomerInfo);
            if(CollectionUtils.isEmpty(list)){
                if(count==0){
                    count++;
                    countFail++;
                }
                inertFlag="1";
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、外部编号对应的客户不存在");
            }
            if("0".equals(inertFlag)){
                TOrderTotal tOrderTotal=this.getLastData();
                String orderNo=codeGenerateService.generateCode("ORDER_TOTAL_CODE_RULE",tOrderTotal.getOrderNo(),tOrderTotal);
                orderTotal.setOrderNo(orderNo);
                orderTotal.setCustomerId(list.get(0).getId());
                orderTotal.setOrderDate(new Date());
                orderTotal.setCreateTime(new Date());
                orderTotal.setUpdateTime(new Date());
                orderTotal.setCreateBy(operName);
                this.insertTOrderTotal(orderTotal);
                successNum++;
                successMsg.append("<br/>" + successNum + "外部客户编号 " + orderTotal.getCustomerNo() + " 导入成功");
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + countFail + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
